import java.util.ArrayList;
import java.util.List;

public class Test41 {
    public static List<Integer> findNumbersWithSum(int[] data,int sum){
        List<Integer> res = new ArrayList<>();

        if (data == null ||data.length < 2) return res;

        int start = 0;
        int end = data.length-1;
        long cursum;
        while (start < end){
            cursum = data[start] + data[end];
            if (cursum == sum) {
                res.add(data[start]);
                res.add(data[end]);
                break;
            }else if (cursum > sum){
                end --;
            }else
                start ++;
        }
        return  res;
    }

    public static List<List<Integer>> findContinuousSequence(int sum){
        List<List<Integer>> res = new ArrayList<>();
        if (sum < 3){
            return res;
        }
        int samll = 1;
        int big = 2;
        int mid = (1+sum)/2;
        int cumsum = samll + big;

        while(samll < mid){
            if (cumsum == sum){
                List<Integer> list = new ArrayList<>();
                for(int i = samll;i <= big; i++){
                    list.add(i);
                }
                res.add(list);
            }
            while (cumsum > sum && samll < mid){
                cumsum -= samll;
                samll ++;
                if (cumsum == sum){
                    List<Integer> list = new ArrayList<>();
                    for(int i = samll;i <= big; i++){
                        list.add(i);
                    }
                    res.add(list);
                }
            }
            big++;
            cumsum += big;
        }
        return  res;
    }

    public static void main(String[] args) {
        int[] data1 = {1, 2, 4, 7, 11, 15};
        System.out.println(findNumbersWithSum(data1, 15));

        System.out.println(findContinuousSequence(15));
    }
}
